package com.ali.Reggie.controller;

import com.ali.Reggie.common.R;
import com.ali.Reggie.entity.Employee;
import com.ali.Reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/6 16:06
 * @description：用户控制类
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * create by: ALi
     * description: 员工登录
     * create time: 2022/6/6 16:24
     *
      * @Param: null
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1.密码转为md5
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据数据库查密码
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(employeeLambdaQueryWrapper);

        //对结果判空
        if(emp == null){
            return R.error("登录失败！");
        }

        //4.密码对比
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误，登录失败!!");
        }

        //查看账号状态
        if(emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);

    }

    //退出方法
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }

    /**
     * create by: ALi
     * description: 新增员工
     * * create time: 2022/6/7 15:32
     *
      * @Param: null
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //初始密码md5加密
        String StringMD5 = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(StringMD5);

//        employee.setCreateTime(LocalDateTime.now());
//
//        employee.setUpdateTime(LocalDateTime.now());
//
//        employee.setCreateUser((Long)request.getSession().getAttribute("employee"));
//
//        employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    //查询数据
    @GetMapping("/page")
    public R<Page> showAll(int page, int pageSize,String name){

        Page page1 = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page1,queryWrapper);

        return R.success(page1);
    }

    //启用和禁用按钮
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        log.info("新增员工，员工信息:{}",employee.toString());

//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);

        employeeService.updateById(employee);

        return R.success("成功新增员工");
    }

    //修改展示数据
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee == null){
            return R.error("没有查询到对饮员工信息");
        }
        return R.success(employee);
    }



}